﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.LinqToSQLDAL
{
    /// <summary>
    /// Linq To SQL DAL for announcements
    /// </summary>
    public class Announcements : IAnnouncements 
    {
        DB _db;

        /// <summary>
        /// Default constructor
        /// </summary>
        public Announcements()
        {
            _db = new DB();
        }

        /// <summary>
        /// Constructor used to pass in a datacontext
        /// </summary>
        /// <param name="db">datacontext for the DAL to use</param>
        public Announcements(DB db)
        {
            _db = db;
            if (_db == null)
                throw new InvalidOperationException("DB cannot be null.");
        }

        /// <summary>
        /// Method to get all announcements
        /// </summary>
        /// <returns>List of Announcements</returns>
        public IQueryable<Announcement> GetAllAnnouncements()
        {
            return from a in _db.cs_Announcements
                   select new Announcement
                   {
                       ID = a.AnnouncementID,
                       Title = a.Title,
                       Description = a.Description,
                       ImageID = a.ImageID,
                       AlbumID = a.AlbumID,
                       Author = a.Author,
                       AnnouncementDate = a.AnnouncementDate,
                   };
        }

        /// <summary>
        /// Method to get all visible announcements
        /// </summary>
        /// <returns>List of Announcements</returns>
        public IQueryable<Announcement> GetVisibleAnnouncements()
        {
            return from a in _db.cs_Announcements
                   where a.AnnouncementDate <= DateTime.Now
                   select new Announcement {
                       ID = a.AnnouncementID,
                         Title = a.Title,
                         Description = a.Description,
                         ImageID = a.ImageID,
                         AlbumID = a.AlbumID,
                         Author = a.Author,
                       AnnouncementDate = a.AnnouncementDate,
                   };
        }

        /// <summary>
        /// Method to get the most recent announcements
        /// </summary>
        /// <param name="count">Number of announcements to return</param>
        /// <returns>List of Announcements</returns>
        public IQueryable<Announcement> GetRecentAnnouncements(int count)
        {
            return (from a in _db.cs_Announcements
                    where a.AnnouncementDate <= DateTime.Now
                    orderby a.AnnouncementDate descending  
                    select new Announcement
                    {
                        ID = a.AnnouncementID,
                       Title = a.Title,
                       Description = a.Description,
                       ImageID = a.ImageID,
                       AlbumID = a.AlbumID,
                       Author = a.Author,
                        AnnouncementDate = a.AnnouncementDate,
                    }).Take(count);
        }

        /// <summary>
        /// Method to get an announcement by the announcement id
        /// </summary>
        /// <param name="id">ID of the announcement to return</param>
        /// <returns>An announcement business entitiy</returns>
        public Announcement GetAnnouncementByID(int id)
        {
            return (from a in _db.cs_Announcements
                    where a.AnnouncementID == id
                    select new Announcement
                    {
                        ID = a.AnnouncementID,
                        Title = a.Title,
                        Description = a.Description,
                        ImageID = a.ImageID,
                        AlbumID = a.AlbumID,
                        Author = a.Author,
                        AnnouncementDate = a.AnnouncementDate,
                    }).SingleOrDefault();
        }

        /// <summary>
        /// Method to insert or update an announcement
        /// </summary>
        /// <param name="announcement">announcement to insert or update</param>
        public void Save(Announcement announcement)
        {
            using (DB db = new DB())
            {
                cs_Announcement dbAnnouncement = db.cs_Announcements.Where(x => x.AnnouncementID == announcement.ID).SingleOrDefault();
                bool isNew = false;
                if (dbAnnouncement == null)
                {
                    dbAnnouncement = new cs_Announcement();
                    isNew = true;
                }

                dbAnnouncement.Title = announcement.Title;
                dbAnnouncement.Description = announcement.Description;
                dbAnnouncement.ImageID = announcement.ImageID;
                dbAnnouncement.AlbumID = announcement.AlbumID;
                dbAnnouncement.Author = announcement.Author;
                dbAnnouncement.AnnouncementDate = announcement.AnnouncementDate;

                if (isNew)
                    db.cs_Announcements.InsertOnSubmit(dbAnnouncement);

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// Method to delete an announcement
        /// </summary>
        /// <param name="id">ID of the announcement to delete</param>
        public void Delete(int id)
        {
            cs_Announcement dbAnnouncement = _db.cs_Announcements.Where(x => x.AnnouncementID == id).SingleOrDefault();

            if (dbAnnouncement != null)
            {
                _db.cs_Announcements.DeleteOnSubmit(dbAnnouncement);
                _db.SubmitChanges();
            }
        }
    }
}
